//
//  SISSBottomRecommandView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/3/29.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSBottomRecommendView.h"


@interface SISSBottomRecommendView ()

/**
 数字label
 */
@property (weak, nonatomic) UILabel *numbersLabel;
/**
 推荐已选客户label
 */
@property (weak, nonatomic) UILabel *recommandLabel;
/**
 手势
 */
@property (weak, nonatomic) UITapGestureRecognizer *tapGesture;

@end
@implementation SISSBottomRecommendView


- (void)setNumbers:(NSInteger)numbers {
    _numbers = numbers;
    
    // 赋值
    self.numbersLabel.text = [NSString stringWithFormat:@"%zd",numbers];
    
    if (numbers > 0) {
        self.backgroundColor = COLOR(0x41B4CD);
        self.numbersLabel.textColor = COLOR(0x33A3BB);
    } else {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        self.numbersLabel.textColor = [UIColor whiteColor];
    }
}

- (void)setTitle:(NSString *)title {
    _title = title;
    
    self.recommandLabel.text = title;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        
        [self setupUI];
        
        // 添加手势
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
        self.tapGesture = tapGesture;
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}

- (void)tapGestureAction:(UITapGestureRecognizer *)tap {
    
    if (self.numbers > 0) {
        if ([self.delegate respondsToSelector:@selector(clickCheckedRecommendViewAction)]) {
            
            [self.delegate clickCheckedRecommendViewAction];
        }
    }
}


/**
 设置UI
 */
- (void)setupUI {
    
    UILabel *numbersLabel = [[UILabel alloc] init];
   
    numbersLabel.backgroundColor = [UIColor groupTableViewBackgroundColor];
    numbersLabel.textAlignment = NSTextAlignmentCenter;
    numbersLabel.font = [UIFont systemFontOfSize:16];
    numbersLabel.textColor = [UIColor whiteColor];
    numbersLabel.text = @"0";
    [self addSubview:numbersLabel];
    
    UILabel *recommandLabel = [[UILabel alloc] init];
    recommandLabel.font = [UIFont systemFontOfSize:16];
    recommandLabel.textColor = [UIColor whiteColor];
    recommandLabel.text = @"推荐已选客源";
    [self addSubview:recommandLabel];
    
    // 自动布局
    NSDictionary *attribute = @{NSFontAttributeName: [UIFont systemFontOfSize:16]};
    CGFloat  width = [numbersLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 16)  options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin   attributes:attribute context:nil].size.width + 10;
    numbersLabel.layer.cornerRadius = width * 0.5;
    numbersLabel.layer.masksToBounds = YES;
    [numbersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(recommandLabel.mas_left).offset(-8);
        make.size.mas_equalTo(CGSizeMake(width, width));
    }];
    
    [recommandLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
    }];
    
    // 记录控件
    self.numbersLabel = numbersLabel;
    self.recommandLabel = recommandLabel;
}



@end
